<?php


namespace app\controller\admin;

use app\model\AdminClass;
use app\model\AdminDormitory;
use app\model\AdminSchool;
use app\model\AdminStaff;
use app\model\AdminStudentDormitory;
use think\Response;

class PublicApi extends Base
{
    /*学校列表*/
    public function SchoolList(): Response {
        $limit = $this->request->get('size', 1000);
        $start = $this->request->get('page', 1);
        $keywords = $this->request->get('name', '');
        $sortKey = $this->request->get('sortKey', 'create_time');
        $order = $this->request->get('order', 'DESC');

        $obj = (new AdminSchool())->where('is_delete', 0);
        if ($keywords) {
            $obj = $obj->whereLike('name', "%{$keywords}%");
        }
        $listObj = $obj->order($sortKey, $order)
            ->paginate(['page' => $start, 'list_rows' => $limit])->toArray();

        return $this->buildSuccess([
            'list'  => $listObj['data'],
            'count' => $listObj['total']
        ]);
    }

    /*楼栋列表*/
    public function DormitoryList() : Response {
        $limit = $this->request->get('size', 1000);
        $start = $this->request->get('page', 1);
        $school_id = $this->request->get('school_id', $this->userInfo['school_id']);
        $keywords = $this->request->get('name', '');
        $sortKey = $this->request->get('sortKey', 'create_time');
        $order = $this->request->get('order', 'DESC');

        $where = [
            ['ad.is_delete', '=', 0],
            ['s.is_delete', '=', 0],
        ];

        $obj = (new AdminDormitory())->alias('ad')
            ->join('admin_school s', 'ad.school_id = s.id');
        if ($keywords) {
            array_push($where, ['ad.name', 'like', "%{$keywords}%"]);
        }
        if ($school_id > 0) {
            array_push($where, ['ad.school_id', '=', $school_id]);
        }
        $listObj = $obj->where($where)
            ->field('ad.id,ad.name,ad.school_id,s.name school_name,ad.create_time')
            ->order('ad.'.$sortKey, $order)
            ->paginate(['page' => $start, 'list_rows' => $limit])->toArray();

        return $this->buildSuccess([
            'list'  => $listObj['data'],
            'count' => $listObj['total']
        ]);
    }

    /*宿舍列表*/
    public function StudentDormitoryList() : Response {
        $limit = $this->request->get('size', 1000);
        $start = $this->request->get('page', 1);
        $school_id = $this->request->get('school_id', $this->userInfo['school_id']);
        $dormitory_id = $this->request->get('dormitory_id', -1);
        $keywords = $this->request->get('name', '');
        $sortKey = $this->request->get('sortKey', 'create_time');
        $order = $this->request->get('order', 'DESC');

        $where = [
            'AdminStudentDormitory.is_delete' => 0
        ];

        $obj = (new AdminStudentDormitory())->alias('AdminStudentDormitory');
        if ($keywords) {
            $obj = $obj->whereLike('name', "%{$keywords}%");
        }
        if ($school_id > 0) {
            $where['AdminStudentDormitory.school_id'] = $school_id;
        }
        if ($dormitory_id != -1) {
            $where['AdminStudentDormitory.dormitory_id'] = $dormitory_id;
        }

        $listObj = $obj->where($where)
            ->order('AdminStudentDormitory.'.$sortKey, $order)
            ->paginate(['page' => $start, 'list_rows' => $limit])->toArray();

        return $this->buildSuccess([
            'list'  => $listObj['data'],
            'count' => $listObj['total']
        ]);
    }

    /*职工列表*/
    public function StaffList(): Response {
        $limit = $this->request->get('size', 1000);
        $start = $this->request->get('page', 1);
        $school_id = $this->request->get('school_id', $this->userInfo['school_id']);
        $type = $this->request->get('type', '');
        $sortKey = $this->request->get('sortKey', 'create_time');
        $order = $this->request->get('order', 'DESC');

        $where = [
            ['AdminStaff.is_delete', '=', 0],
            ['AdminStaff.type', '<>', AdminStaff::STUDENT]
        ];
        $obj = (new AdminStaff())->alias('AdminStaff');
        if ($school_id > 0) {
            array_push($where, ['AdminStaff.school_id', '=', $school_id]);
        }
        if ($type) {
            array_push($where, ['AdminStaff.type', '=', $type]);
        }
        $listObj = $obj->where($where)
            ->append(['status_text','type_text'])
            ->order('AdminStaff.'.$sortKey, $order)
            ->paginate(['page' => $start, 'list_rows' => $limit])->toArray();

        return $this->buildSuccess([
            'list'  => $listObj['data'],
            'count' => $listObj['total']
        ]);
    }

    /*班级列表*/
    public function ClassList(): Response {
        $limit = $this->request->get('size', 1000);
        $start = $this->request->get('page', 1);
        $school_id = $this->request->get('school_id', $this->userInfo['school_id']);
        $sortKey = $this->request->get('sortKey', 'create_time');
        $order = $this->request->get('order', 'DESC');

        $where = [
            ['is_delete', '=', 0]
        ];
        $obj = (new AdminClass());
        if ($school_id > 0) {
            array_push($where, ['school_id', '=', $school_id]);
        }
        $listObj = $obj->where($where)
            ->order($sortKey, $order)
            ->paginate(['page' => $start, 'list_rows' => $limit])->toArray();

        return $this->buildSuccess([
            'list'  => $listObj['data'],
            'count' => $listObj['total']
        ]);
    }
}